import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

import static com.zh.freechat.common.CustomConst.PINYIN_FORMAT;

/**
 * @author ZH
 * @date 12:15 2022/11/28
 */
public class PinyinDemo {
    private static HanyuPinyinOutputFormat format;

    @BeforeAll
    public static void prepareFormat(){
        //1.创建一个格式化输出对象
        format = new HanyuPinyinOutputFormat();
        //2.设置好格式
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    }

    @Test
    public void testPinyinChar() throws BadHanyuPinyinOutputFormatCombination {
        char ch='重';
        String[] str = PinyinHelper.toHanyuPinyinStringArray(ch,format);
        //3.打印输出
        System.out.println(Arrays.toString(str));
    }

    @Test
    public void testPinyinString() throws BadHanyuPinyinOutputFormatCombination {
        String ch="重庆,长沙";
        String str = PinyinHelper.toHanYuPinyinString(ch,format,"_",true);
        //3.打印输出
        System.out.println(str);
    }

    @Test
    public void testFirstLetter () throws BadHanyuPinyinOutputFormatCombination {
        String name = "小明";
        char first = name.charAt(0);
        if (first>'\u4e00' && first<'\u9fa5')  {
            String[] str = PinyinHelper.toHanyuPinyinStringArray(first,PINYIN_FORMAT);
            System.out.println(Arrays.toString(str));
        } else {
            System.out.println(first);
        }
    }
}
